package com.finalcial.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.finalcial.po.LoginMsg;
import com.finalcial.service.ApplicationService;
import com.finalcial.service.DeclarationInformationService;
import com.finalcial.service.ReadService;
import com.finalcial.util.PageUtil;
/**
 * 
 * @author Lenovo
 *待阅
 */
@Controller
@RequestMapping("/read")
public class ReadingController {
	
	@Resource
	private ReadService readService;
	@Autowired
	private DeclarationInformationService declarationInformationService;
	@Autowired
	private ApplicationService applicationService;
	
	@RequestMapping("/myReading")
	public String listWorkMsg(HttpServletRequest request, HttpServletResponse response,HttpSession httpSession) {
		String workStateName = "待阅";//来自界面
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userId = loginMsg.getUserId();	
		List<Map<String, Object>> readinglist = null;
		String pageNow = request.getParameter("pageNow");
		PageUtil page = null;
		Integer totalCount = readService.listReadMsgCounts(userId, workStateName);//总条数
		if(pageNow != null) {
			page = new PageUtil(totalCount, Integer.parseInt(pageNow));
			readinglist = readService.listReadMsg(userId, workStateName, page.getStartPos(), page.getPageSize());
		}else {
			page = new PageUtil(totalCount, 1);
			readinglist = readService.listReadMsg(userId, workStateName, page.getStartPos(), page.getPageSize());
		}
		for(Map<String, Object> map:readinglist) {
			for(String s :map.keySet()) {
				System.out.println(map.get(s));
			}
		}
		request.setAttribute("page", page);
		request.setAttribute("readinglist", readinglist);
		return "datamanager/personwork/myReading";
	}
	/**
	 * 待阅补单详细信息---参数(单子ID：reimbursementId,标识ID：msgId)
	 * msgId：不同的值跳转不同的页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getSingleApprovaMsgs")
	public String getSingleApprovaMsgs(HttpServletRequest request, HttpServletResponse response) {
		String reimbursementId = request.getParameter("reimbursementId");
//		String msgId = request.getParameter("msgId");
		String reimbursementTypeName = request.getParameter("reimbursementTypeName");
		/*try {
			reimbursementTypeName = new String(reimbursementTypeName.getBytes("iso-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}*/
		String[] arrReimbursementTypeName = reimbursementTypeName.split("-");
		System.out.println(arrReimbursementTypeName[0]);
		reimbursementTypeName = arrReimbursementTypeName[0];
		
		//补单信息
		List<Map<String, Object>> singleSupplement = readService.getSingleSupplement(reimbursementId);
		//基本人信息
		List<Map<String, Object>> listdetails = declarationInformationService.getEssentialInformation(reimbursementId);
		//内部人信息
		List<Map<String, Object>> listOtherInformation = declarationInformationService.getOtherInformation(reimbursementId);
		//显示所有可选择的接收者角色
		List<Map<String, Object>> roleNameList = applicationService.showRoleNameList();
		for(Map<String, Object> map:listdetails) {
			for(String s :map.keySet()) {
				System.out.println(map.get(s));
			}
		}
		//解析图片文件
		List<Map<String, Object>> imageList = applicationService.getCheckSheet(reimbursementId);
		request.setAttribute("roleNameList", roleNameList);
		request.setAttribute("listOtherInformation", listOtherInformation);
		request.setAttribute("listdetails", listdetails);
		request.setAttribute("singleSupplement", singleSupplement);
		request.setAttribute("imageList", imageList);
		String str = "";
		if("国内差旅".endsWith(reimbursementTypeName)) {//国内差旅
			str = "datamanager/personwork/domestictravelSingleapproval";
		}
		if("物业管理".endsWith(reimbursementTypeName)) {//物业管理
			str = "datamanager/personwork/propertyManagerApplySingleapproval";
		}
		if("员工福利".endsWith(reimbursementTypeName)) {//员工福利
			str = "datamanager/personwork/staffFormApplySingleapproval";
		}
		if("公务交通".endsWith(reimbursementTypeName)) {//公务交通
			str = "datamanager/personwork/officialtrafficSingleapproval";
		}
		return str;
	}
	

	//多条件查询已阅
		@ResponseBody
		@RequestMapping("/chooseReading")
		public List<Map<String, Object>> getWorkDoneMsg(HttpServletResponse response,HttpServletRequest request,HttpSession httpSession){
			String workStateName = "待阅";
			LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
			Integer userId = loginMsg.getUserId();	
			String userName = request.getParameter("userName");
			String startTime = request.getParameter("startTime");
			//System.out.println(startTime);
			String endTime = request.getParameter("endTime");
			//System.out.println(endTime);
			List<Map<String, Object>> chooseReadingList = readService.getReadMsg(userId, workStateName, userName, startTime, endTime);
			for(Map<String, Object> map:chooseReadingList) {
				for(String s :map.keySet()) {
					System.out.println(map.get(s));
				}
			}
			
			return chooseReadingList;
			
		}
}
